﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using AutoMapper;
using DataTransferObject;
using DomainModel;
using ServiceLayer;

namespace RemoteFacade
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class SignalementManagementRemoteService : ISignalementManagementRemoteService
    {
        private string _currentUser;

        public void OpenSession(string userName)
        {
            _currentUser = userName;
            Console.WriteLine(">> Opening session for user '{0}'", _currentUser);

        }

        public void CloseSession()
        {
            Console.WriteLine(">> Closing session for user '{0}'", _currentUser);
        }

        public void CreateManySignalement(SignalementGroupDTO pannes)
        {
            var signalementManagementService = new SignalementManagementService();
            foreach (var info in pannes.Details)
            {
                signalementManagementService.CreateSignalement(_currentUser, info.ObjetPatrimonialId, info.PanneId,
                                                         pannes.ObservationDate, info.Comments);
            }
        }

        public List<SignalementDTO> GetPendingSignalements()
        {
            var signalementManagementService = new SignalementManagementService();
            var pendingSignalements = signalementManagementService.GetPendingSignalements();
            var signalements = Mapper.Map<List<Signalement>, List<SignalementDTO>>(pendingSignalements);
            return signalements;
        }

        public CreateSignalementDTO GetCreateSignalementDatas()
        {
            var signalementManagementService = new SignalementManagementService();
            var allObjetPatrimonials = signalementManagementService.GetAllObjetPatrimonials();
            var allPannes = signalementManagementService.GetAllPannes();

            var reportingPannesDatasDTO = new CreateSignalementDTO
                                              {
                                                  ObjetPatrimonials =
                                                      Mapper.Map<List<ObjetPatrimonial>, List<ObjetPatrimonialDTO>>(
                                                          allObjetPatrimonials),
                                                  Pannes = Mapper.Map<List<Panne>, List<PanneDTO>>(allPannes)
                                              };
            return reportingPannesDatasDTO;
        }
    }
}
